package com.spa.interfaces.common;

import cn.dev33.satoken.annotation.SaIgnore;
import com.spa.application.dto.wx.ResultDTO;
import com.spa.application.dto.wx.WXConfigDTO;
import com.spa.application.query.wx.GetOpenIdQuery;
import com.spa.application.query.wx.InitConfigQuery;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.service.wx.VerdureRequestWrapper;
import com.spa.infrastructure.service.wx.WXJsSdkService;
import com.spa.infrastructure.service.wx.WXLoginService;
import com.spa.infrastructure.service.wx.WXPayServiceV3;
import com.spa.infrastructure.service.wx.result.AccessTokenResult;
import com.spa.interfaces.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("wx")
@Slf4j
public class WXController extends BaseController {

    @Autowired
    private WXLoginService wxLoginService;

    @Autowired
    private WXJsSdkService wxJsSdkService;

    @Autowired
    private WXPayServiceV3 wxPayServiceV3;


    @PostMapping("redirect")
    public ResponseMessage<String> redirect(@RequestBody GetOpenIdQuery query) throws IOException {
        AccessTokenResult accessTokenResult = wxLoginService.getAccessToken(query.getCode());
        return responseHelper.success(accessTokenResult.getOpenid());
    }

    /**
     * 用户端-首页-获取微信的初始化配置
     * @param query
     * @return
     * @throws IOException
     */
    @PostMapping("getInitConfig")
    @SaIgnore
    public ResponseMessage<WXConfigDTO> getInitConfig(@RequestBody InitConfigQuery query) throws IOException {
        return responseHelper.success(wxJsSdkService.sign(query.getUrl()));
    }

    @PostMapping("payNotifyV3")
    public ResultDTO payNotifyV3(HttpServletRequest request) throws Exception {
        VerdureRequestWrapper requestWrapper = new VerdureRequestWrapper(request);
        //平台证书序列号
        String serial = requestWrapper.getHeader("Wechatpay-Serial");
        //应答签名
        String signature = requestWrapper.getHeader("Wechatpay-Signature");
        String signType = requestWrapper.getHeader("wechatpay-signature-type");
        //应答随机串
        String nonce = requestWrapper.getHeader("Wechatpay-Nonce");
        //应答时间戳
        String timestamp = requestWrapper.getHeader("Wechatpay-Timestamp");
        String body = requestWrapper.getRequestParams();
        com.wechat.pay.java.core.notification.RequestParam requestParam = new com.wechat.pay.java.core.notification.RequestParam.Builder()
                .body(body)
                .serialNumber(serial)
                .nonce(nonce)
                .signType(signType)
                .timestamp(timestamp)
                .signature(signature)
                .build();
        return wxPayServiceV3.notification(requestParam);
    }

}




